package org.client.iot.ui.account;

import android.util.Patterns;

import org.client.iot.LauncherApplication;
import org.client.iot.R;
import org.client.iot.databinding.ActivitySettingBinding;
import org.client.iot.domain.CaptchaImageDto;
import org.client.iot.util.MMKVUtils;
import org.tzw.template.base.BaseActivity;

public class SettingActivity extends BaseActivity<ActivitySettingBinding> {

    @Override
    protected void initView() {
        setSupportActionBar(mRoot.toolbarDevSetting.toolbarPageHead);
        mRoot.toolbarDevSetting.textViewTitle.setText("设置");
        String ip = (String) MMKVUtils.getInstance().get("ip", "");
        String port = (String) MMKVUtils.getInstance().get("port", "");
        mRoot.etIp.setText(ip == null ? "" : ip);
        mRoot.etPort.setText(port == null ? "" : port);
    }

    @Override
    protected void initListener() {
        mRoot.toolbarDevSetting.imgBack.setOnClickListener(v -> {
            finish();
        });
        mRoot.btnSave.setOnClickListener(v -> {
            String ip = mRoot.etIp.getText().toString().trim();
            String port = mRoot.etPort.getText().toString().trim();
            if (ip.isEmpty() || port.isEmpty()) {
                showToastLong("请填写IP和端口");
                return;
            }
            boolean bo = Patterns.WEB_URL.matcher(ip).matches();
            if (!bo) {
                showToastLong("请填写正确的IP");
                return;
            }
            MMKVUtils.getInstance().put("ip", ip);
            MMKVUtils.getInstance().put("port", port);
            showToastShort("保存成功");
            LauncherApplication.initNetWork(getApplication());
        });
    }

}